using System;
using Subsync.Core.Sync;

namespace Subsync.GUI
{
	/// <summary>
	/// Author: Steven Goh Jian Wen
	/// </summary>
	public class NewSyncManager
	{
        //Class Variables
		private string path_to_folder;
		private string sync_name;
		private string description;
		private bool auto_manage_conflicts;
		private string icon;
		private bool custom_repo;
		private string custom_repo_path;
		private bool has_errors = false;
		private string error_description = "";

        //Class Properties
        public bool HasErrors
        {
            get { return has_errors; }
        }

        public string ErrorMsg
        {
            get { return error_description; }
        }
		
		/// <summary>
		/// Always valid since selected by GUI.
		/// </summary>
		/// <param name="path_to_folder">
		/// A <see cref="System.String"/>
		/// </param>
		public SyncAddError Step1 (string path_to_folder)
		{
            if (!Subsync.GUI.Tools.FolderExists(path_to_folder))
            {
                return SyncAddError.FolderDoesNotExist;
            }
            if (Subsync.GUI.Tools.IsVersioned(path_to_folder))
            {
                return SyncAddError.WorkingCopyPathError;
            }
            if (Subsync.GUI.Tools.IsInsideAnotherWorkingCopy(path_to_folder))
            {
                return SyncAddError.WorkingCopyPathError;
            }
			this.path_to_folder = path_to_folder;
			return SyncAddError.Ok;
		}

		public SyncAddError Step2 (string sync_name)
		{
			if (Tools.SyncExists (sync_name)) 
				return SyncAddError.SyncNameExists;
			if (sync_name.Trim() == "") 
				return SyncAddError.EmptyString;
			this.sync_name = sync_name.Trim();
			return SyncAddError.Ok;
		}

		public SyncAddError Step3 (string description)
		{
			this.description = description;
			return SyncAddError.Ok;
		}

		public SyncAddError Step4 (bool conflict)
		{
			this.auto_manage_conflicts = conflict;
			return SyncAddError.Ok;
		}

		public SyncAddError Step5 (bool custom_repo, string custom_repo_string)
		{
			this.custom_repo = custom_repo;
			this.custom_repo_path = custom_repo_string;
			return SyncAddError.Ok;
		}

		public SyncAddError Step6 (string icon_path)
		{
			this.icon = icon_path;
			return SyncAddError.Ok;
		}

		void ResetErrors ()
		{
			this.error_description = "";
			this.has_errors = false;
		}
		
		public void Execute ()
		{
			ResetErrors ();
			
			Sync s = new Sync ();
			s.name = this.sync_name;
			s.description = this.description;
			s.working_copy_path = this.path_to_folder;
			s.auto_conflict_management = this.auto_manage_conflicts;
			s.icon = this.icon;
			try {
				if (this.custom_repo)
					s.NewSync (this.custom_repo_path);
				else
					s.NewSync ();
			}
			catch (SyncNotFound) {
				this.has_errors = true;
				this.error_description = "Sync not found.";
			} catch (FolderDoesNotExist) {
				this.has_errors = true;
				this.error_description = "Folder does not exist.";
			} catch (RepositoryNotFound) {
				this.has_errors = true;
				this.error_description = "Repository cannot be found.";
			} catch (SyncNameConflict) {
				this.has_errors = true;
				this.error_description = "Sync name conflicts.";
			} catch (PathAlreadyAWorkingCopy) {
				this.has_errors = true;
				this.error_description = "Directory is a SVN working copy, cannot proceed.";
			} catch (DiskSpaceException) {
				this.has_errors = true;
				this.error_description = "Disk is full, please clear some space and try again.";
			} catch (Exception) {
				this.has_errors = true;
				this.error_description = "An unknown error has occurred.";
			}
		}
	}
}
